package hackathon.light.up;

import android.graphics.Color;
import android.util.Log;

import com.e3roid.drawable.Shape;

public class Light {

	private Shape light;

	public int start_row, start_col, end_row, end_col, color_id, thickness;
	private LightDirection direction;
	int next_row,next_col;

	public Light(int thickness, int start_row, int start_col, int end_row,
			int end_col, int colId) {

		this.start_row = start_row;
		this.start_col = start_col;
		this.end_col = end_col;
		this.end_row = end_row;
		this.color_id = colId;
		this.thickness = thickness;

		calculateDirection();
		drawLight();
	}

	public Shape getShape(){
		return light;
	}
	
	public void drawLight() {
		float angle;
		int startX = Grid.POSX + Grid.CELL_SIZE/2, startY = Grid.POSY + Grid.CELL_SIZE/2, width, height;
		switch (direction) {
		case E:
			startY += start_row * Grid.CELL_SIZE - Grid.THICKNESS;
			startX += start_col * Grid.CELL_SIZE;
			height = thickness;
			width = (int) ((end_col - start_col) * Grid.CELL_SIZE);
			angle = 0;
			break;
		case W:
			startY += end_row * Grid.CELL_SIZE - Grid.THICKNESS;
			startX += end_col * Grid.CELL_SIZE;
			height = thickness;
			width = (int) ((start_col - end_col) * Grid.CELL_SIZE);
			angle = 0;
			break;
		case S:
			startY += start_row * Grid.CELL_SIZE;
			startX += start_col * Grid.CELL_SIZE - Grid.THICKNESS;
			width = thickness;
			height = (int) ((end_row - start_row) * Grid.CELL_SIZE);
			angle = 0;
			break;
		case N:
			startY += end_row * Grid.CELL_SIZE;
			startX += end_col * Grid.CELL_SIZE - Grid.THICKNESS;
			width = thickness;
			height = (int) ((start_row - end_row) * Grid.CELL_SIZE);
			angle = 0;
			break;
		case NE:
			startY += end_row * Grid.CELL_SIZE -  Grid.CELL_SIZE*(start_row - end_row)*( Math.sqrt(2) -1 )/2 - Grid.THICKNESS/2;
			startX += end_col * Grid.CELL_SIZE  - (end_col - start_col) *Grid.CELL_SIZE/2  - Grid.THICKNESS/2;
			width = thickness;
			height = (int) ((start_row - end_row) *Math.sqrt(2)* Grid.CELL_SIZE);
			angle = 45;
			break;
			
		case SW:
			startY += start_row * Grid.CELL_SIZE -  Grid.CELL_SIZE*(end_row - start_row)*( Math.sqrt(2) -1 )/2 - Grid.THICKNESS/2;
			startX += start_col * Grid.CELL_SIZE  - (start_col - end_col) *Grid.CELL_SIZE/2  - Grid.THICKNESS/2;
			width = thickness;
			height = (int) ((end_row - start_row) *Math.sqrt(2)* Grid.CELL_SIZE);
			angle = 45;
			break;
			
		case NW:
			startY += end_row * Grid.CELL_SIZE -  Grid.CELL_SIZE*(start_row - end_row)*( Math.sqrt(2) -1 )/2;
			startX += start_col * Grid.CELL_SIZE  - (start_col - end_col) *Grid.CELL_SIZE/2  - Grid.THICKNESS/2;
			width = thickness;
			height = (int) ((start_row - end_row) *Math.sqrt(2)* Grid.CELL_SIZE);
			angle = -45;
			break;
			
		case SE:
			startY += start_row * Grid.CELL_SIZE -  Grid.CELL_SIZE*(end_row - start_row)*( Math.sqrt(2) -1 )/2;
			startX += end_col * Grid.CELL_SIZE  - (end_col - start_col) *Grid.CELL_SIZE/2  - Grid.THICKNESS/2;
			width = thickness;
			height = (int) ((end_row -start_row) *Math.sqrt(2)* Grid.CELL_SIZE);
			angle = -45;
			break;
			
		default:
			startY += start_row * Grid.CELL_SIZE;
			startX += start_col * Grid.CELL_SIZE;
			height = thickness;
			width = (int) ((end_col - start_col) * Grid.CELL_SIZE);
			angle = 0;
			break;
		}

		light = new Shape(startX, startY, width, height);
		int color = LightColor.get_color(color_id);
		light.setColor(Color.red(color)/255,Color.green(color)/255,Color.blue(color)/255,1);
		
		light.rotate(angle);

		Log.d("LIGHT :", startX + " " + startY+ " "+ width+ " "+ height);

	}

	public LightDirection getDirection() {
		return this.direction;
	}
	
	private LightDirection calculateDirection() {

		if (start_col == end_col) {
			if (start_row < end_row) {
				direction = LightDirection.S;
			} else {
				direction = LightDirection.N;
			}
		} else {
			if (start_col < end_col) {
				if (start_row == end_row) {
					direction = LightDirection.E;
				} else {
					if (start_row < end_row) {
						direction = LightDirection.SE;
					} else {
						direction = LightDirection.NE;
					}
				}
			} else if (start_row == end_row) {
				direction = LightDirection.W;
			} else {
				if (start_row < end_row) {
					direction = LightDirection.SW;
				} else {
					direction = LightDirection.NW;
				}
			}
		}
		Log.d("LIGHT","direction: "+ direction+"");
		next_row = MapDirection.getNextRow(direction);
		next_col = MapDirection.getNextCol(direction);
		return direction;
	}
}
